package com.study.file.service;

import com.study.file.dao.FileDao;
import com.study.main.vo.ConditionPagination;
import com.study.main.vo.UserInfo;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author 谭永超 [www.tanyongchao.tk:8090/SumDome/index]
 * @Date 2018/9/10 23:04
 * @Description
 */
@Service(value = "downloadService")
public class DownloadServiceImpl implements DownloadService {
    @Autowired
    @Qualifier(value = "fileDao")
    private FileDao fileDao;

    @Override
    public String download(String url, ConditionPagination cp) {
        int[] d = cp.getDis();
        if(d == null){
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("0");
        List<UserInfo> userInfoList = fileDao.selectAll(cp);
        Row row = sheet.createRow(0);
        CellStyle cellStyle = workbook.createCellStyle();
        row.createCell(0).setCellStyle(cellStyle);
        row.createCell(0).setCellValue("中文名");
        row.createCell(1).setCellStyle(cellStyle);
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellStyle(cellStyle);
        row.createCell(2).setCellValue("gitlab账号");
        row.createCell(3).setCellStyle(cellStyle);
        row.createCell(3).setCellValue("密码");
        row.createCell(4).setCellStyle(cellStyle);
        row.createCell(4).setCellValue("电话号码");
        row.createCell(5).setCellStyle(cellStyle);
        row.createCell(5).setCellValue("账户");
        row.createCell(6).setCellStyle(cellStyle);
        row.createCell(6).setCellValue("学分");
        row.createCell(7).setCellStyle(cellStyle);
        row.createCell(7).setCellValue("成绩");
        int i = 1;
        for (UserInfo userInfo : userInfoList) {
            row = sheet.createRow(i);
            cellStyle = workbook.createCellStyle();
                row.createCell(0).setCellStyle(cellStyle);
                row.createCell(0).setCellValue(userInfo.getChineseName());
                row.createCell(1).setCellStyle(cellStyle);
                row.createCell(1).setCellValue(userInfo.getEmail());
                row.createCell(2).setCellStyle(cellStyle);
                row.createCell(2).setCellValue(userInfo.getGitlabNumber());
                row.createCell(3).setCellStyle(cellStyle);
                row.createCell(3).setCellValue(userInfo.getPassword());
                row.createCell(4).setCellStyle(cellStyle);
                row.createCell(4).setCellValue(userInfo.getTelNumber());
                row.createCell(5).setCellStyle(cellStyle);
                row.createCell(5).setCellValue(userInfo.getUserName());
                row.createCell(6).setCellStyle(cellStyle);
                row.createCell(6).setCellValue(userInfo.getCredit());
                row.createCell(7).setCellStyle(cellStyle);
                row.createCell(7).setCellValue(userInfo.getGrade());

            i++;
        }
        workbook.setSheetName(0, "信息");
        try {
            Date date = new Date();
            DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String name = dateFormat.format(date);
            File file = new File(url + "/" + name + ".xls");
            FileOutputStream fileoutputStream = new FileOutputStream(file);
            workbook.write(fileoutputStream);
            fileoutputStream.close();
            return name+".xls";
        } catch (IOException e) {
            return "";
        }
    }else{
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("0");
            Row row = sheet.createRow(0);
            CellStyle cellStyle = workbook.createCellStyle();
            row.createCell(0).setCellStyle(cellStyle);
            row.createCell(0).setCellValue("中文名");
            row.createCell(1).setCellStyle(cellStyle);
            row.createCell(1).setCellValue("邮箱");
            row.createCell(2).setCellStyle(cellStyle);
            row.createCell(2).setCellValue("gitlab账号");
            row.createCell(3).setCellStyle(cellStyle);
            row.createCell(3).setCellValue("密码");
            row.createCell(4).setCellStyle(cellStyle);
            row.createCell(4).setCellValue("电话号码");
            row.createCell(5).setCellStyle(cellStyle);
            row.createCell(5).setCellValue("账户");
            row.createCell(6).setCellStyle(cellStyle);
            row.createCell(6).setCellValue("学分");
            row.createCell(7).setCellStyle(cellStyle);
            row.createCell(7).setCellValue("成绩");
            for (int i = 0; i<d.length;i++) {
                row = sheet.createRow(i+1);
                UserInfo userInfo = fileDao.findUserInfoById(d[i]);
                cellStyle = workbook.createCellStyle();
                row.createCell(0).setCellStyle(cellStyle);
                row.createCell(0).setCellValue(userInfo.getChineseName());
                row.createCell(1).setCellStyle(cellStyle);
                row.createCell(1).setCellValue(userInfo.getEmail());
                row.createCell(2).setCellStyle(cellStyle);
                row.createCell(2).setCellValue(userInfo.getGitlabNumber());
                row.createCell(3).setCellStyle(cellStyle);
                row.createCell(3).setCellValue(userInfo.getPassword());
                row.createCell(4).setCellStyle(cellStyle);
                row.createCell(4).setCellValue(userInfo.getTelNumber());
                row.createCell(5).setCellStyle(cellStyle);
                row.createCell(5).setCellValue(userInfo.getUserName());
                row.createCell(6).setCellStyle(cellStyle);
                row.createCell(6).setCellValue(userInfo.getCredit());
                row.createCell(7).setCellStyle(cellStyle);
                row.createCell(7).setCellValue(userInfo.getGrade());
            }
            workbook.setSheetName(0, "信息");
            try {
                Date date = new Date();
                DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                String name = dateFormat.format(date);
                File file = new File(url + "/" + name + ".xls");
                FileOutputStream fileoutputStream = new FileOutputStream(file);
                workbook.write(fileoutputStream);
                fileoutputStream.close();
                return name+".xls";
            } catch (IOException e) {
                return "";
            }
        }
    }
}
